package cn.txun.csmall.product.controller;

import cn.txun.csmall.commons.pojo.vo.PageData;
import cn.txun.csmall.commons.web.JsonResult;
import cn.txun.csmall.product.pojo.param.CategoryAddNewParam;
import cn.txun.csmall.product.pojo.param.CategoryUpdateInfoParam;
import cn.txun.csmall.product.pojo.vo.AlbumListItemVO;
import cn.txun.csmall.product.pojo.vo.CategoryListItemVO;
import cn.txun.csmall.product.service.ICategoryService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 处理类别相关请求的控制器类
 *
 * @author java@txun.cn
 * @version 0.0.1
 */
@Slf4j
@RestController
@RequestMapping("/categories")
@Validated
@Api(tags = "01. 类别管理模块")
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    public CategoryController() {
        log.debug("创建控制器类对象：CategoryController");
    }

    // http://localhost:9180/categories/add-new
    @PostMapping("/add-new")
    @ApiOperation("添加类别")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid CategoryAddNewParam categoryAddNewParam) {
        log.debug("开始处理【添加类别】的请求，参数：{}", categoryAddNewParam);
        categoryService.addNew(categoryAddNewParam);
        return JsonResult.ok();
    }

    // http://localhost:9180/categories/9527/delete
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除类别")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult delete(@PathVariable @Range(min = 1, message = "请提交有效的类别ID值！") Long id) {
        log.debug("开始处理【根据ID删除类别】的请求，参数：{}", id);
        categoryService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:9180/categories/9527/update
    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改类别详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult updateInfoById(@PathVariable @Range(min = 1, message = "请提交有效的类别ID值！") Long id,
                                     @Valid CategoryUpdateInfoParam categoryUpdateInfoParam) {
        log.debug("开始处理【修改类别详情】的业务，参数：{}", categoryUpdateInfoParam);
        categoryService.updateInfoById(id, categoryUpdateInfoParam);
        return JsonResult.ok();
    }

    // http://localhost:9180/categories/9527/enable
    @PostMapping("/{id:[0-9]+}/enable")
    @ApiOperation("启用类别")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult setEnable(@PathVariable @Range(min = 1, message = "请提交有效的类别ID值！") Long id) {
        log.debug("开始处理【启用类别】的请求，参数：{}", id);
        categoryService.setEnable(id);
        return JsonResult.ok();
    }

    // http://localhost:9180/categories/9527/disable
    @PostMapping("/{id:[0-9]+}/disable")
    @ApiOperation("禁用类别")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult setDisable(@PathVariable @Range(min = 1, message = "请提交有效的类别ID值！") Long id) {
        log.debug("开始处理【禁用类别】的请求，参数：{}", id);
        categoryService.setDisable(id);
        return JsonResult.ok();
    }
    //localhost:9180/categories/{parentId}
    @GetMapping("/{parentId:[0-9]+}")
    @ApiOperation("根据父级id查询类别列表")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId",value = "父级id", dataType = "integer")
    })
    public JsonResult list(@Range(min=0,message = "查询类别列表失败,请提供正确的类别id")
                           @PathVariable  Long parentId){
        log.debug("开始处理【根据父级id查询类别列表】的请求，参数为{}，",parentId);
        if(parentId==null || parentId<0){
            parentId=0L;
        }
        List<CategoryListItemVO> categoryListItemVOS = categoryService.listByParentId(parentId);
        log.debug("处理【根据父级id查询类别列表】的请求，完成！数据：{}",categoryListItemVOS);;
        return JsonResult.ok(categoryListItemVOS);
    }

}